package com.xiaobaibai.config;


import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;

import java.time.Duration;

@Configuration
@EnableCaching
public class RedisCacheConfig {

    @Bean
    public RedisCacheConfiguration setRedisCacheConfiguration(){

        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();

        //修改value的序列化方式
        //记得前方要重新赋值,因为方法返回的是一个新的config
        config=config.serializeValuesWith
                (RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

        config=config.entryTtl(Duration.ofDays(1));//设置过期时间为1天

        //设置缓存的前缀
        config=config.prefixKeysWith("cache:");

        return config;
    }

}
